Slient Blog

RxJava RxLifecycle基础

2017-03-14

在前面的文章中我们没有处理过解除订阅的问题,因此会导致大量的内存泄漏,我也没有提到,因为这篇文章要说的东西帮助我们很好的解决了这个问题

开源项目

RxLifecycle 地址:https://github.com/trello/RxLifecycle

导入依赖

1
2
compile 'com.trello:rxlifecycle:0.3.1'
compile 'com.trello:rxlifecycle-components:0.3.1'

RxLifecycle为我们提供了可以直接继承的RxAppCompatActivity,RxActivity,RxFragment,RxDialogFragment等等,可以很方便的让组件生命周期和Rx生命周期绑定。

我们直接观看官方的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class RxlifecycleTest extends RxAppCompatActivity {
private static final String TAG = "RxLifecycleAndroid";
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.rxlifecy_main);
Log.d(TAG,"onCreat()");
// Specifically bind this until onPause()
Observable.interval(1, TimeUnit.SECONDS)
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.i(TAG, "Unsubscribing subscription from onCreate()");
}
})
.compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.i(TAG, "Started in onCreate(), running until onPause(): " + num);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
// Using automatic unsubscription, this should determine that the correct time to
// unsubscribe is onStop (the opposite of onStart).
Observable.interval(1, TimeUnit.SECONDS)
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.i(TAG, "Unsubscribing subscription from onStart()");
}
})
.compose(this.<Long>bindToLifecycle())
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.i(TAG, "Started in onStart(), running until in onStop(): " + num);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
// `this.<Long>` is necessary if you're compiling on JDK7 or below.
//
// If you're using JDK8+, then you can safely remove it.
Observable.interval(1, TimeUnit.SECONDS)
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.i(TAG, "Unsubscribing subscription from onResume()");
}
})
.compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.i(TAG, "Started in onResume(), running until in onDestroy(): " + num);
}
});
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}

我们来看log

1
2
3
4
5
6
7
8
9
10
11
12
13
03-14 21:10:48.794 13598-14121/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onStart(), running until in onStop(): 0
03-14 21:10:48.871 13598-14123/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onResume(), running until in onDestroy(): 0
03-14 21:10:53.792 13598-14121/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onStart(), running until in onStop(): 1
03-14 21:10:53.870 13598-14123/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onResume(), running until in onDestroy(): 1
03-14 21:10:58.791 13598-14121/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onStart(), running until in onStop(): 2
03-14 21:10:58.871 13598-14123/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onResume(), running until in onDestroy(): 2
03-14 21:11:03.792 13598-14121/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onStart(), running until in onStop(): 3
03-14 21:11:03.870 13598-14123/com.androidlab.rxpack I/RxLifecycleAndroid: Started in onResume(), running until in onDestroy(): 3
03-14 21:12:22.796 13598-13598/com.androidlab.rxpack D/RxLifecycleAndroid: onPause()
03-14 21:12:23.174 13598-13598/com.androidlab.rxpack I/RxLifecycleAndroid: Unsubscribing subscription from onStart()
03-14 21:12:23.175 13598-13598/com.androidlab.rxpack D/RxLifecycleAndroid: onStop()
03-14 21:12:23.175 13598-13598/com.androidlab.rxpack I/RxLifecycleAndroid: Unsubscribing subscription from onResume()
03-14 21:12:23.176 13598-13598/com.androidlab.rxpack D/RxLifecycleAndroid: onDestroy()

根据代码可以看出两种使用方法

  • 手动设置取消订阅的生命周期

    1
    .compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
  • 手动绑定,自动取消

    1
    .compose(this.<Long>bindToLifecycle())

那我们现在在看两个个优雅的示例

1
2
3
4
5
RxView.clicks(mBtnRxRegister)
.throttleFirst(2, TimeUnit.SECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.compose(this.bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(action);
1
2
3
4
RxBus.getInstance()
.toObserverable(UserBean.class)
.compose(this.bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(action1);

以上就是一些最基本的使用了。

重要的事情说一遍“这里的compose一定要在subscribe上一个执行”

Tags: Android
使用微信添加

若你觉得我的文章对你有帮助,请添加我为好友

扫描二维码,分享此文章